import UpdateTag from '../update-tag/update-tag.vue'
import Pages from '../../pages/pages.vue'
import { showMsg, getHotPortfolioList, deleteHotPortfolio, swapHotPortfolio } from '../../../vuex/action'

export default {
  components: {
    'vue-update-tag': UpdateTag,
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      showUpdateTagModal: false,
      updateTagForm: {
        betaId: '',
        // name: '',
        titleTag: '',
        styleTag: '',
        waveTag: '',
        marketValueTag: ''
      }
    }
  },
  ready() {
    this.searchList(0)
  },
  vuex: {
    getters: {
      fresh: state => state.hotPortfolioListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetlist: getHotPortfolioList,
      xDelete: deleteHotPortfolio,
      xSwap: swapHotPortfolio
    }
  },
  methods: {
    searchList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetlist({
        start: start,
        limit: limit
      })
        .then((res) => {
          this.list = res.data.data.list
          this.pages.totalPages = Math.ceil(res.data.data.totalCount / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    deleteIt(id) {
      this.xDelete({
        betaId: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    swap(fromId, toId) {
      this.xSwap({
        fromBetaId: fromId,
        toBetaId: toId
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateTag(it) {
      this.updateTagForm.betaId = it.betaId
      this.updateTagForm.titleTag = it.titleTag
      this.updateTagForm.styleTag = it.styleTag
      this.updateTagForm.waveTag = it.waveTag
      this.updateTagForm.marketValueTag = it.marketValueTag
      this.showUpdateTagModal = true
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.searchList({})
      }
    }
  }
}
